package com.wym.charging.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wym.charging.dao.UserDao;
import com.wym.charging.dto.BuyMonthlySubscriptionDto;
import com.wym.charging.entity.MonthlySubscriptionOption;
import com.wym.charging.entity.MonthlySubscriptionPurchaseRecord;
import com.wym.charging.entity.RechargeRecord;
import com.wym.charging.entity.User;
import com.wym.charging.service.MonthlySubscriptionOptionService;
import com.wym.charging.service.MonthlySubscriptionPurchaseRecordService;
import com.wym.charging.service.RechargeRecordService;
import com.wym.charging.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author WangYimin 2024年12月06日 15:29:46
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private RechargeRecordService rechargeRecordService;
    @Autowired
    private MonthlySubscriptionOptionService monthlySubscriptionOptionService;
    @Autowired
    private MonthlySubscriptionPurchaseRecordService monthlySubscriptionPurchaseRecordService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public User recharge(Integer userId, BigDecimal amount) throws Exception {
        User user = getById(userId);
        if (user == null) {
            throw new Exception("用户不存在");
        }
        user.setAccountBalance(user.getAccountBalance().add(amount));
        updateById(user);
        RechargeRecord rechargeRecord = new RechargeRecord();
        rechargeRecord.setUserId(userId);
        rechargeRecord.setRechargeAmount(amount);
        rechargeRecordService.save(rechargeRecord);
        return user;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void buyMonthlySubscription(BuyMonthlySubscriptionDto dto) {
        User user = getById(dto.getUserId());
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        if (user.getMonthlySubscription()) {
            throw new RuntimeException("已经购买月度订阅,请在有效期过后购买");
        }
        MonthlySubscriptionOption monthlySubscriptionOption = monthlySubscriptionOptionService.getById(dto.getMonthlySubscriptionOptionId());
        if (monthlySubscriptionOption == null) {
            throw new RuntimeException("月度订阅选项不存在");
        }
        if (user.getAccountBalance().compareTo(monthlySubscriptionOption.getPrice()) < 0) {
            throw new RuntimeException("余额不足");
        }
        user.setAccountBalance(user.getAccountBalance().subtract(monthlySubscriptionOption.getPrice()));
        user.setMonthlySubscription(true);
        user.setRemainingTime(monthlySubscriptionOption.getMaximumChargingDuration() * 60);
        user.setSubscriptionEndTime(LocalDateTime.now().plusDays(monthlySubscriptionOption.getValidityPeriod() * 30));
        updateById(user);
        MonthlySubscriptionPurchaseRecord monthlySubscriptionPurchaseRecord = new MonthlySubscriptionPurchaseRecord();
        monthlySubscriptionPurchaseRecord.setUserId(user.getId());
        monthlySubscriptionPurchaseRecord.setAmountSpent(monthlySubscriptionOption.getPrice());
        monthlySubscriptionPurchaseRecord.setGearPosition(monthlySubscriptionOption.getValidityPeriod());
        monthlySubscriptionPurchaseRecordService.save(monthlySubscriptionPurchaseRecord);
    }
}
